﻿using MaterialDesignThemes.Wpf;
using StarryEdge.Menu.SettingSystem.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarryEdge.Menu.SettingSystem.ViewModels.Dialog
{
    public class PluginHistoryPopupDialog
    {
        public PluginFileInfoModel SelectedItem { get; set; }
        public ObservableCollection<PluginFileInfoModel> Files { get; set; }
        private string _name;
        public PluginHistoryPopupDialog(string name)
        {
            Files = new ObservableCollection<PluginFileInfoModel>();
            DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, "Backup"));
            foreach (FileInfo fileInfo in directoryInfo.GetFiles())
            {
                if (fileInfo.Name.Contains(name))
                {
                    Files.Add(new PluginFileInfoModel()
                    {
                        Info = fileInfo,
                        FileTime = fileInfo.CreationTime.ToString("G"),
                        Name = fileInfo.Name.Split('_')[0],
                    });
                }
            }
            _name = name;
        }

        public Prism.Commands.DelegateCommand Verify => new Prism.Commands.DelegateCommand(() =>
        {
            if (SelectedItem != null)
            {
                File.Move(SelectedItem.Info.FullName, Path.Combine(Environment.CurrentDirectory, "Update", $"{_name}.7z"));
                DialogHost.Close("Global");
            }
        });
    }
}
